package reflect;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

public class reflectDemo3 {
    public static void main(String[] args) throws NoSuchMethodException, ClassNotFoundException, InvocationTargetException, InstantiationException, IllegalAccessException {
        Person p = new Person("范传奇",40);
        System.out.println(p.toString());


        //获取Person类对象，并且将此类对象放在cls中
        Class cls = Class.forName("reflect.Person");
        //getConstructor方法可以根据你传递过来的参数，找到参数相对应的构造方法
        Constructor c = cls.getConstructor(String.class,int.class);   //根据参数类型，获取到指定类中的构造器
        //利用找到的构造器实例化对象，并且传入参数
        Object o = c.newInstance("山地",8);
        System.out.println(o.toString());

        System.out.println(reflectDemo3.class.getPackage().getName());
    }
}
